@extends('admin.layout.global')

@section('css-plugins')
    <link href="{{ asset('style/plugins/chosen_v1.2.0/chosen.min.css') }}" rel="stylesheet">
    <link href="{{ asset('style/plugins/daterangepicker/daterangepicker.css') }}" rel="stylesheet">
    <link href="{{ asset('style/plugins/toastr/toastr.min.css') }}" rel="stylesheet">
@endsection

@section('css')
    <link href="{{ asset('style/admin/css/custom.css') }}" rel="stylesheet">
    <style>
        .pagination {
            margin: 0 0;
        }
        .form-group {
            margin-bottom: 0;
        }
    </style>
@endsection

@section('js-plugins')
    <script src="{{ asset('style/plugins/chosen_v1.2.0/chosen.jquery.min.js') }}"></script>
    <script src="{{ asset('style/plugins/moment/moment.js') }}"></script>
    <script src="{{ asset('style/plugins/daterangepicker/daterangepicker.js') }}"></script>
    <script src="{{ asset('style/plugins/toastr/toastr.min.js') }}"></script>
@endsection

@section('js')
    <script src="{{ asset('style/admin/js/pages/blankon.form.element.js') }}"></script>
    <script>
        $(function(){
            var menu = $(".dropdown-menu").find("a");
            menu.click(function(){
                var text = $(this).text();
                var k = $(this).data('key');
                $('#showKey').text(text);
                $('#searchType').val(k);
            });
        });
    </script>
    <script>
        $(document).ready(function () {
            //定义
            var $storeID        = $("select[name='storeID']");
            var $payState       = $("select[name='payState']");
            var $logisticsState = $("select[name='logisticsState']");
            var $time           = $("input[name='time']");
            var $searchType     = $("#searchType");
            var $keyword        = $("input[name='keyword']");
            //查询
            function getQuery(getUrl) {
                var storeID         = $storeID.val();
                var payState        = $payState.val();
                var logisticsState  = $logisticsState.val();
                var time            = $time.val();
                var searchType      = $searchType.val();
                var keyword         = $keyword.val();
                var separator       = '?';
                var url = getUrl;
                if (storeID != '')
                {
                    url += separator + 'storeID=' + storeID;
                    separator = '&';
                }
                if (payState != '')
                {
                    url += separator + 'payState=' + payState;
                    separator = '&';
                }
                if (logisticsState != '')
                {
                    url += separator + 'logisticsState=' + logisticsState;
                    separator = '&';
                }
                if (time != '')
                {
                    url += separator + 'time=' + time;
                    separator = '&';
                }
                if (searchType != '')
                {
                    url += separator + 'searchType=' + searchType;
                    separator = '&';
                }
                if (keyword != '')
                {
                    url += separator + 'keyword=' + keyword;
                    separator = '&';
                }
                window.location = url;
            }
            $('#search').click(function () {
                getQuery('{{ action('Admin\Shop\OrderController@getIndex') }}');
            });
            $('#getExcel').click(function () {
                getQuery('{{ action('Admin\Shop\OrderController@getExcel') }}');
            });
            $('#getExcelPrint').click(function () {
                getQuery('{{ action('Admin\Shop\OrderController@getExcelPrint') }}');
            });
            $('#dateTime').daterangepicker({
                "autoUpdateInput": false,
                "timePicker": true,
                "timePicker24Hour": true,
                "timePickerSeconds": true,
                "timePickerIncrement": 1,
                "locale": {
                    "format": "YYYY-MM-DD HH:mm:ss",
                    "separator": " - ",
                    "applyLabel": "提交",
                    "cancelLabel": "取消",
                    "fromLabel": "From",
                    "toLabel": "To",
                    "customRangeLabel": "Custom",
                    "daysOfWeek": [
                        "日",
                        "一",
                        "二",
                        "三",
                        "四",
                        "五",
                        "六"
                    ],
                    "monthNames": [
                        "一月",
                        "二月",
                        "三月",
                        "四月",
                        "五月",
                        "六月",
                        "七月",
                        "八月",
                        "九月",
                        "十月",
                        "十一月",
                        "十二月"
                    ],
                    "firstDay": 1,
                },
                "drops": "down"
            }, function(start, end, label) {
            });
            $('#dateTime').on('apply.daterangepicker', function(ev, picker) {
                $(this).val(picker.startDate.format('YYYY-MM-DD HH:mm:ss') + " - " + picker.endDate.format('YYYY-MM-DD HH:mm:ss'));
                getQuery('{{ action('Admin\Shop\OrderController@getIndex') }}');
            });
            @if(isset($_GET['time']) && $_GET['time'] != '')
            @else
            $('#dateTime').val('');
            @endif
            $('.inputSelect').change(function(){
                getQuery('{{ action('Admin\Shop\OrderController@getIndex') }}');
            });
        });
    </script>
    <script>
        $(function(){
            toastr.options = {
                "progressBar": true,
                "positionClass": "toast-top-right",
                "timeOut": "1200",
            }
            var menu = $(".dropdown-menu").find("a");
            menu.click(function(){
                var text = $(this).text();
                var k = $(this).data('key');
                $('#showKey').text(text);
                $('#searchType').val(k);
            });
            $('.btn-group button').click(function(){
                var $this   =   $(this);
                var id      =   $this.data('id');
                var state   =   $this.data('state');
                $.ajax({
                    url: "{{ action('Admin\Shop\OrderController@postLogisticsState') }}",
                    data: { id:id,state:state,_token:'{{ csrf_token() }}'},
                    type:'post',
                    success: function (data) {
                        var jsonData = JSON.parse(data);
                        if (jsonData.code)
                        {
                            $this.siblings().removeClass('btn-danger btn-info btn-success');
                            $this.siblings().addClass('btn-default');
                            $this.removeClass('btn-default');
                            switch (state)
                            {
                                case 0:
                                    $this.addClass('btn-danger');
                                    break;
                                case 1:
                                    $this.addClass('btn-info');
                                    break;
                                case 2:
                                    $this.addClass('btn-success');
                                    break;
                            }
                            toastr.success(jsonData.message);
                        } else {
                            toastr.error(jsonData.message);
                        }
                    }
                })
            });
        });
    </script>
@endsection


@section('content')
    <div class="body-content animated fadeIn">
        <div class="row">
            <div class="col-md-12">
                <!-- Start inline form -->
                <div class="panel rounded shadow">
                    <div class="panel-body no-padding">
                        <div class="form-horizontal mt-10">
                            <div class="form-body">
                                <div class="form-group">
                                    <div class="col-sm-2">
                                        <select data-placeholder="选择店铺" name="storeID" class="chosen-select mb-15 inputSelect" tabindex="2">
                                            <option value="">选择店铺</option>
                                            @foreach($store as $v)
                                                <option value="{{ $v->id }}" @if(isset($_GET['storeID']) && $_GET['storeID'] == $v->id) selected @endif>{{ $v->name }}</option>
                                            @endforeach
                                        </select>
                                    </div>
                                    <div class="col-sm-2">
                                        <select data-placeholder="付款状态" name="payState" class="chosen-select mb-15 inputSelect" tabindex="2">
                                            <option value="">付款状态</option>
                                            <option value="0" @if(isset($_GET['payState']) && $_GET['payState'] == 0) selected @endif>未付款</option>
                                            <option value="1" @if(isset($_GET['payState']) && $_GET['payState'] == 1) selected @endif>已付款</option>
                                        </select>
                                    </div>
                                    <div class="col-sm-2">
                                        <select data-placeholder="配送状态" name="logisticsState" class="chosen-select mb-15 inputSelect" tabindex="2">
                                            <option value="">配送状态</option>
                                            <option value="0" @if(isset($_GET['logisticsState']) && $_GET['logisticsState'] == 0) selected @endif>未配送</option>
                                            <option value="1" @if(isset($_GET['logisticsState']) && $_GET['logisticsState'] == 1) selected @endif>配送中</option>
                                            <option value="2" @if(isset($_GET['logisticsState']) && $_GET['logisticsState'] == 2) selected @endif>已配送</option>
                                        </select>
                                    </div>
                                    <div class="col-sm-2">
                                        <input id="dateTime" class="form-control inputSelect" name="time" type="text" value="@if(isset($_GET['time']) && $_GET['time'] != ''){{ $_GET['time'] }}@endif" placeholder="订单创建时间" />
                                    </div>
                                    <div class="col-sm-4">
                                        <div class="input-group mb-15">
                                            <div class="input-group-btn">
                                                <?php
                                                function getKeyName($name){
                                                    switch ($name){
                                                        case 'orderNumber':
                                                            $keyName = '订单号';
                                                            break;
                                                        case 'uid':
                                                            $keyName = '用户ID';
                                                            break;
                                                        case 'address':
                                                            $keyName = '配送信息';
                                                            break;
                                                        default:
                                                            $keyName = '订单号';
                                                    }
                                                    return $keyName;
                                                }
                                                ?>
                                                <button data-toggle="dropdown" class="btn btn-primary dropdown-toggle" type="button"><span id="showKey">{{ isset($_GET['searchType']) ? getKeyName($_GET['searchType']) : '订单号' }}</span> <span class="caret"></span></button>
                                                <ul class="dropdown-menu">
                                                    <li><a data-key="orderNumber">订单号</a></li>
                                                    <li><a data-key="uid">用户ID</a></li>
                                                    <li><a data-key="address">配送信息</a></li>
                                                </ul>
                                            </div>
                                            <input id="searchType" name="searchType" value="{{ isset($_GET['searchType']) ? $_GET['searchType'] : 'orderNumber' }}" type="hidden">
                                            <input name="keyword" value="{{ isset($_GET['keyword']) ? $_GET['keyword'] : '' }}" class="form-control no-border-left" type="text">
                                            <span class="input-group-btn"><button type="button" class="btn btn-success" id="search"><i id="icoico" class="fa fa-search"></i></button></span>
                                        </div>
                                    </div>
                                </div><!-- /.form-group -->
                                <div class="form-group">
                                    <div class="col-sm-2">
                                        总金额：{{ $sum }} 円
                                    </div>
                                    <div class="col-sm-2">
                                        <button class="btn btn-primary" id="getExcel">导出Excel</button>
                                    </div>
                                    <div class="col-sm-2">
                                        <button class="btn btn-warning" id="getExcelPrint">导出打印信息</button>
                                    </div>
                                    <div class="col-sm-6">
                                    </div>
                                </div><!-- /.form-group -->
                            </div><!-- /.form-body -->
                        </div>

                    </div><!-- /.panel-body -->
                </div><!-- /.panel -->
                <!--/ End inline form -->
                <!-- Start basic color table -->
                <div class="panel">
                    <div class="panel-heading">
                        <div class="clearfix"></div>
                    </div><!-- /.panel-heading -->
                    <div class="panel-body no-padding">
                        <div class="table-responsive" style="margin-top: -1px;">
                            <table class="table table-striped table-success">
                                <thead>
                                <tr>
                                    <th class="text-center border-right" style="width: 5%">ID</th>
                                    <th>订单号</th>
                                    <th>店铺</th>
                                    <th>总价</th>
                                    <th>用户ID</th>
                                    <th>所购商品</th>
                                    <th>配送信息</th>
                                    <th>配送时间</th>
                                    <th>备注</th>
                                    <th>付款状态</th>
                                    <th>配送状态</th>
                                    <th>创建时间</th>
                                </tr>
                                </thead>
                                <tbody>
                                @foreach($order as $v)
                                    <tr>
                                        <td class="text-center border-right">{{ $v->id }}</td>
                                        <td>{{ $v->orderNumber }}</td>
                                        <td>{{ isset($store[$v->storeID]) ? $store[$v->storeID]->name : '未知' }}</td>
                                        <td>{{ $v->amount }}</td>
                                        <td>{{ $v->uid }}</td>
                                        <?php
                                            $v->orderList = json_decode($v->orderList) ? json_decode($v->orderList) : $v->orderList;
                                            if(is_string($v->orderList))
                                            {
                                                $orderList = $v->orderList;
                                            }else{
                                                $orderList = [];
                                                foreach ($v->orderList as $vv)
                                                {
                                                    if($vv->thumb != '')
                                                    {
                                                        array_push($orderList, $vv->title.' x '.$vv->num);
                                                    }else{
                                                        if(isset($vv->discount))
                                                        {
                                                            array_push($orderList, $vv->title.' '.$vv->discount.' 折');
                                                        }else{
                                                            array_push($orderList, $vv->title.' '.$vv->price.' 円');
                                                        }
                                                    }
                                                }
                                                $orderList = implode(',', $orderList);
                                            }
                                        ?>
                                        <td>{{ $orderList }}</td>
                                        <td>{{ $v->address }}</td>
                                        <td>{{ $v->shopTime }}</td>
                                        <td>{{ $v->remark }}</td>
                                        <td>@if($v->payState == 0)<span class="label label-danger">未付款</span>@else<span class="label label-success">已付款</span>@endif</td>
                                        <td><div class="btn-group btn-group-xs" role="group" aria-label="...">
                                                <button type="button" data-id="{{ $v->id }}" data-state="0" class="btn @if($v->logisticsState == 0) btn-danger @else btn-default @endif">未配送</button>
                                                <button type="button" data-id="{{ $v->id }}" data-state="1" class="btn @if($v->logisticsState == 1) btn-info @else btn-default @endif">配送中</button>
                                                <button type="button" data-id="{{ $v->id }}" data-state="2" class="btn @if($v->logisticsState == 2) btn-success @else btn-default @endif">已配送</button>
                                            </div></td>
                                        <td>{{ $v->created_at }}</td>
                                    </tr>
                                @endforeach
                                </tbody>
                            </table>
                        </div><!-- /.table-responsive -->
                    </div><!-- /.panel-body -->
                    <div class="panel-footer">
                        <div class="row">
                            <div class="col-md-12">
                                <div style="text-align: center">
                                    <?php
                                        if(isset($_GET['storeID']))
                                            $order->appends(['storeID' => $_GET['storeID']]);
                                        if(isset($_GET['payState']))
                                            $order->appends(['payState' => $_GET['payState']]);
                                        if(isset($_GET['logisticsState']))
                                            $order->appends(['logisticsState' => $_GET['logisticsState']]);
                                        if(isset($_GET['time']))
                                            $order->appends(['time' => $_GET['time']]);
                                        if(isset($_GET['searchType']))
                                            $order->appends(['searchType' => $_GET['searchType']]);
                                        if(isset($_GET['keyword']))
                                            $order->appends(['keyword' => $_GET['keyword']]);
                                    ?>
                                    {!! $order->render() !!}
                                </div>
                            </div>
                        </div>
                    </div>
                </div><!-- /.panel -->
                <!--/ End basic color table -->

            </div><!-- /.col-md-12 -->
        </div>
    </div>
@endsection